home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Modules / BackSpaceModules / Source / Swarm / SwarmView.m < prev    next >
Text File  |  1991-12-01  |  6KB  |  274 lines

  1. /*
  2. ** XSwarm
  3. ** ======
  4. **
  5. ** Purpose:    Display a swarm of bees chasing a wasp.
  6. **
  7. ** Features:    uses only integer math
  8. **        has no redeeming social value
  9. **
  10. ** Comments:    Most of this program is greatly rewritten by me, but the
  11. **        initial math and update stuff is left over from the older
  12. **        program.  Much of the NeXT related stuff is from LizardSaver.
  13. **
  14. ** Created:    by Jeff Butterworth on 7/11/90
  15. **        butterwo@cs.unc.edu
  16. **
  17. ** NeXTPoRT:    Kurt Werle
  18. **              frsvnsvn!kurt@crash.cts.com
  19. **
  20. ** Updated:    by Kurt Werle on 10/28/91
  21. **
  22. */
  23.  
  24. /* Standard Includes */
  25. #import "Thinker.h"
  26. #import <appkit/NXImage.h>
  27. #import <appkit/Panel.h>                // for NXRunAlertPanel()
  28. //#import <appkit/appkit.h>
  29. #import <dpsclient/wraps.h>
  30. #import <libc.h>
  31. #import <math.h>
  32.  
  33. /* For those of you who want to know what version you are using. */
  34.  
  35. /* Includes for this project. */
  36. #import "SwarmView.h"
  37.  
  38. @implementation SwarmView
  39.  
  40. - initFrame:(NXRect *)frameRect
  41. {
  42.     [super initFrame:frameRect];
  43.     [self newSize];
  44.     [NXApp loadNibSection:"SwarmPrefs.nib" owner:self];
  45.     winNum=[[self window] windowNum];
  46.  
  47.     return self;
  48. }
  49.  
  50. - sizeTo:(NXCoord)width :(NXCoord)height
  51. {
  52.     [super sizeTo:width :height];
  53.     mywidth = width;
  54.     myheight = height;
  55.     return self;
  56. }
  57.  
  58. - newSize
  59. {
  60.     int b;
  61.     
  62.     /* Get the random number generator ready. */
  63.     srandom(getpid());
  64.     
  65.     times = TIMES;        /* number of time steps recorded  */
  66.     newbees = bees = BEES;    /* number of bees */
  67.     wasp_vel = WASPVEL;    /* maximum wasp speed */
  68.     bee_vel = BEEVEL;    /* maximum bee speed */
  69.     wasp_acc = WASPACC;    /* maximum wasp acceleration */
  70.     bee_acc = BEEACC;    /* bee acceleration */
  71.     timeout = 0;        /* time in seconds before screen saving */
  72.     border = BORDER;    /* border limiting wasp travel */
  73.     verbose = FALSE;    /* display settings if TRUE */
  74.     stay_in_front = FALSE;    /* Try to stay in clear area of the screen. */
  75.     root = FALSE;        /* display in root window */
  76.     cursortime = 0;        /* Number of updates to follow the cursor */
  77.  
  78.     /*alloc the mem*/
  79.     x = (int *) malloc(sizeof(int) * MAXBEES * times);
  80.     y = (int *) malloc(sizeof(int) * MAXBEES * times);
  81.     xv = (int *) malloc(sizeof(int) * MAXBEES);
  82.     yv = (int *) malloc(sizeof(int) * MAXBEES);
  83.     /* wasp */
  84.     wx[0] = BORDER + random() % (int) (mywidth - 2*BORDER);
  85.     wy[0] = BORDER + random() % (int) (myheight - 2*BORDER);
  86.     wx[1] = wx[0];
  87.     wy[1] = wy[0];
  88.     wxv = 0;
  89.     wyv = 0;
  90.  
  91.     /* set up bees */
  92.     for (b = 0 ; b < MAXBEES ; b++)
  93.     {
  94.         X(0,b) = random() % 1000;   /* Fudge */
  95.         /*X(0,b) = random() % mywidth;  Why can't I use this?*/
  96.         X(1,b) = X(0,b);
  97.         Y(0,b) = random() % 1000;   /* Fudge */
  98.         /*Y(0,b) = random() % myheight; Why can't I use this?*/
  99.         Y(1,b) = Y(0,b);
  100.         xv[b] = RAND(7);
  101.         yv[b] = RAND(7);
  102.         /* Initialize point positions, velocities, etc. */
  103.  
  104.     }
  105.  
  106.     return self;
  107. }
  108.  
  109. /* Animate()                                */
  110. /* Move the swarm around.                        */
  111.  
  112. - oneStep
  113. {
  114.     int            b;        /* bee index */
  115.     int            dx,dy,distance;
  116.  
  117.     bees = newbees;
  118.     /* These variables are related to mouse control of the wasp. */
  119.  
  120.  
  121.  
  122.     /* <=- Wasp -=> */
  123.     /* Age the position arrays. */
  124.     wx[2] = wx[1];
  125.     wx[1] = wx[0];
  126.     wy[2] = wy[1];
  127.     wy[1] = wy[0];
  128.     
  129.     PScurrentmouse(winNum, &curX, &curY);
  130.  
  131.     if (cursortime)   /* If there's time left on the cursor count */
  132.         {
  133.         wx[0] = (int) curX;   wy[0] = (int) curY;
  134.         }
  135.     else
  136.         {
  137.             
  138.         /* Accelerate */
  139.         wxv += RAND(wasp_acc);
  140.         wyv += RAND(wasp_acc);
  141.         /* Speed Limit Checks */
  142.         if (wxv > wasp_vel) wxv = wasp_vel;
  143.         if (wxv < -wasp_vel) wxv = -wasp_vel;
  144.         if (wyv > wasp_vel) wyv = wasp_vel;
  145.         if (wyv < -wasp_vel) wyv = -wasp_vel;
  146.  
  147.         /* Move */
  148.         wx[0] = wx[1] + wxv;
  149.         wy[0] = wy[1] + wyv;
  150.  
  151.         /*         Bounce Checks */
  152.         if ((wx[0] < border))
  153.             {
  154.             wxv = abs(wxv);
  155.             }
  156.         if ((wy[0] < border))
  157.             {
  158.             wyv = abs(wyv);
  159.             }
  160.         if ((wx[0] > mywidth-border-1))
  161.             {
  162.             wxv = -(abs(wxv));
  163.             }
  164.         if ((wy[0] > myheight-border-1))
  165.             {
  166.             wyv = -(abs(wyv));
  167.             }
  168.         }
  169.     /* Test the cursor */
  170.     if (mywidth - curX + myheight - curY <= 10.0)
  171.         [self show]; 
  172.  
  173.     /* Don't let things settle down. */
  174.     xv[random() % bees] += RAND(3);
  175.     yv[random() % bees] += RAND(3);
  176.  
  177.     /* <=- Bees -=> */
  178.     for (b = 0 ; b < bees ; b++)
  179.     {
  180.         /* Age the arrays. */
  181.         X(2,b) = X(1,b);
  182.         X(1,b) = X(0,b);
  183.         Y(2,b) = Y(1,b);
  184.         Y(1,b) = Y(0,b);
  185.  
  186.         /* Accelerate */
  187.         dx = wx[1] - X(1,b);
  188.         dy = wy[1] - Y(1,b);
  189.         distance = abs(dx)+abs(dy); /* approximation */
  190.         if (distance == 0) distance = 1;
  191.         xv[b] += (dx*bee_acc)/distance;
  192.         yv[b] += (dy*bee_acc)/distance;
  193.  
  194.         /* Speed Limit Checks */
  195.         if (xv[b] > bee_vel) xv[b] = bee_vel;
  196.         if (xv[b] < -bee_vel) xv[b] = -bee_vel;
  197.         if (yv[b] > bee_vel) yv[b] = bee_vel;
  198.         if (yv[b] < -bee_vel) yv[b] = -bee_vel;
  199.  
  200.         /* Move */
  201.         X(0,b) = X(1,b) + xv[b];
  202.         Y(0,b) = Y(1,b) + yv[b];
  203.         
  204.  
  205.     }
  206.  
  207.     /* Erase previous, draw current, sync for smoothness. */
  208.  
  209.  
  210.     /* Draw Wasp */
  211.     if (1)
  212.     {
  213.         /*NXRect myrect = {0, 0, mywidth, myheight};
  214.         PSsetgray(0);
  215.         NXRectFill(&myrect);*/
  216.  
  217.         PSsetgray(1.0);
  218.          PSmoveto(wx[0], wy[0]); 
  219.         PSlineto(wx[1], wy[1]);
  220.           PSstroke();
  221.         /**/PSsetgray(0.0);
  222.         PSmoveto(wx[1], wy[1]);
  223.         PSlineto(wx[2], wy[2]);
  224.           PSstroke();/**/
  225.     }
  226.  
  227.     /* Draw Bees */
  228.     {
  229.         PSsetgray(1.0);
  230.         for (b = 0 ; b < bees ; b++)
  231.         {
  232.              PSmoveto(X(0,b), Y(0,b)); 
  233.             PSlineto(X(1,b), Y(1,b));
  234.         }
  235.           PSstroke();
  236.         PSsetgray(0.0);
  237.         for (b = 0 ; b < bees ; b++)
  238.         {
  239.             PSmoveto(X(1,b), Y(1,b));
  240.             PSlineto(X(2,b), Y(2,b));
  241.         }
  242.           PSstroke();/**/
  243.  
  244.     }
  245.     return self;
  246.     
  247. }
  248. - (BOOL) useBufferedWindow
  249. {    return NO;
  250. }
  251. - show
  252.     {
  253.     [myPrefWindow orderFront:nil];
  254.     return self;
  255.     }
  256. - setNumberBees: sender
  257.     {
  258.     NXRect aRect = {0, 0, mywidth, myheight};
  259.     newbees = [sender intValue];
  260.     [beeText takeIntValueFrom:sender];
  261.     [self lockFocus];
  262.     PSsetgray(0.0);
  263.     NXRectFill(&aRect);
  264.     [self unlockFocus];
  265.     return self;
  266.     }
  267. - setFollow: sender
  268.     {    
  269.     cursortime = [sender intValue];
  270.     return self;
  271.     }
  272. @end
  273.  
  274.